local SkillBase = require("gameLogic.fightSkill.show.SkillBase")
local SkillBehavior = class("SkillBehavior", SkillBase)
local GameUtils = require("utils.GameUtils")
local Vector3 = require("utils.Vector3")
local AttributeConst = require("const.AttributeConst")
local FightConst = require("const.FightConst")
-- 执行具体行为的技能

function SkillBehavior:ctor(config)
    self:_init(config)
end

function SkillBehavior:_init(config)
    self._config = config
    self:setId(self._config.id)
    local skillStrategy = require("config.skill_strategy").get(config.strategy_id)
    assert(skillStrategy, "can not found config in skill_strategy by id=" .. config.strategy_id)
    self._strategy = require("gameLogic.fightSkill.strategy.SkillStrategy").new(self, skillStrategy)
end

function SkillBehavior:attack()
    self._entity:setDone(true)
end

function SkillBehavior:strategy(targets)
    self:_effectPlay(targets)
    for i, v in ipairs(targets) do
        self._strategy:doStrategy(v)
    end
end

function SkillBehavior:_playEffect(effectPosition)
    local effectName = self._config.effect
    if effectName == "none" then
        return
    end

    local object = G_ResourceManager:loadEffect(effectName)
    local gameObject = GameUtils.instantiate(object)
    gameObject.transform.position = effectPosition
    local sequence = DOTween.Sequence()
    sequence:AppendInterval(self:getEffectTime())

    sequence:AppendCallback(
        function()
            GameUtils.destroy(gameObject)
        end
    )
end

function SkillBehavior:getEffectTime()
    return self._config.effect_time
end

function SkillBehavior:getHitTime()
    return self._config.hit_time
end

function SkillBehavior:getSkillAction()
    return "Skill"
end

function SkillBehavior:getId()
    return self._id
end

-- 技能目标  begin
function SkillBehavior:getSkillTargets(index)
    local targetName = self._config.skill_renshu
    local targetFunc = self["_renshu_" .. targetName]
    if targetFunc and type(targetFunc) == "function" then
        local manager = self._entity:getManager()
        local targetStanding = self:getSkillTargetStanding()
        local targets = manager:getEnemyEntitys(targetStanding)
        if #targets == 0 then
            return
        end
        return targetFunc(self, index, targets)
    end
end

function SkillBehavior:getSkillTargetStanding()
    local targetStanding = self:getStanding()
    if self._config.skill_target == FightConst.TARGET_FRIEND then -- 技能作用己方
        targetStanding = (targetStanding + 1) % 2
        targetStanding = targetStanding == 0 and 2 or targetStanding
    end
    return targetStanding
end

function SkillBehavior:_renshu_danren(index, targets)
    local target = targets[index]
    return {target}
end

function SkillBehavior:_renshu_yihang(index, targets)
    local target = targets[index]
    local stance = target:getStance()
    local result = {}
    for i = 1, #targets do
        if targets[i]:getStance().row == stance.row then
            table.insert(result, targets[i])
        end
    end
    return result
end

function SkillBehavior:_renshu_yipai(index, targets)
    local target = targets[index]
    local stance = target:getStance()
    local result = {}
    for i = 1, #targets do
        if targets[i]:getStance().col == stance.col then
            table.insert(result, targets[i])
        end
    end
    return result
end

function SkillBehavior:_renshu_quanti(index, targets)
    return targets
end
-- 技能目标  end

-- 技能特效类型 begin
function SkillBehavior:_effectPlay(targets)
    local typeName = self._config.effect_type
    local effectFunc = self["_effect_" .. typeName]
    if effectFunc and type(effectFunc) == "function" then
        effectFunc(self, targets)
    end
end

function SkillBehavior:_effect_single(targets)
    local FightHelper = require("gameLogic.fight.FightHelper")
    local manager = self._entity:getManager()
    local targetPos = manager:getEnemyEntityCenterPos(self._entity:getStanding())
    local offset = FightHelper.getEffectOffset(self._config)
    offset.z = offset.z * FightConst.Z_RATIO[self._entity:getStanding()]
    targetPos = targetPos + offset
    self:_playEffect(targetPos)
end

function SkillBehavior:_effect_multiple(targets)
    for i, v in ipairs(targets) do
        local effectPosition = v.transform.position
        effectPosition.z = effectPosition.z + 0.3
        self:_playEffect(effectPosition)
    end
end
-- 技能特效类型 end

return SkillBehavior
